﻿using Nemesis.WoW.ObjectManager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Nemesis.Plugins.NRelog.Settings
{
    internal class SchedulerSettings
    {




        private static DateTime startTime;


        internal static int timesReloged;
        internal static int relogCount;
        internal static int pauseTime;
        internal static double timeToGo;

        internal static DateTime endTime;
        internal static DateTime restartTime;
        internal static DateTime lastInterval = DateTime.Now;

        internal static bool isRunning;
        internal static bool isOnPause;
        internal static bool endTimeReached;
        internal static bool relogEnabled;

        internal static string bNet = Nemesis.Classes.MainSettings.Instance.RelogBattleNetAccount;
        internal static string login = Nemesis.Classes.MainSettings.Instance.RelogAccountLogin;
        internal static string pw = Nemesis.Classes.MainSettings.Instance.RelogAccountPassword;
        internal static string realm = Nemesis.Classes.MainSettings.Instance.RelogRealm;
        internal static string character = Nemesis.Classes.MainSettings.Instance.RelogCharacter;

        private static bool relogSuccessfull;


        internal static void OnBotStart()
        {
            if (Entry.UI.MaxTime.Text == "" || Entry.UI.MinTime.Text == "")
            {
                Logger.WriteLine("[NRelog] You have to set a min and max runtime!");
                Nemesis.Bot.Engine.Stop();
                return;
            }

            if (relogEnabled && (Entry.UI.MinPause.Text == "" || Entry.UI.MaxPause.Text == ""))
            {
                Logger.WriteLine("[NRelog] The reloger is enabled. You have to set a min and max pause time!");
                Nemesis.Bot.Engine.Stop();
                return;
            }
            if (Entry.UI.RelogCountTextbox.Text == "" || Convert.ToInt32(Entry.UI.RelogCountTextbox.Text) == 0)
            {
                relogCount = 100000;
            }
            else
            {
                relogCount = Convert.ToInt32(Entry.UI.RelogCountTextbox.Text);
            }
            relogEnabled = Entry.UI.RelogCheckbox.Checked;
            lastInterval = DateTime.Now;
            double pauseminutes = 0;
            startTime = DateTime.Now;
            Random r = new Random();
            double minutes = r.NextDouble() * (Convert.ToInt32(Entry.UI.MaxTime.Text) - Convert.ToInt32(Entry.UI.MinTime.Text)) + Convert.ToInt32(Entry.UI.MinTime.Text);
            endTime = startTime.AddMinutes(minutes);

            if (Entry.UI.MaxPause.Text != "" && Entry.UI.MinPause.Text != "")
            {
                pauseminutes = r.NextDouble() * (Convert.ToInt32(Entry.UI.MaxPause.Text) - Convert.ToInt32(Entry.UI.MinPause.Text)) + Convert.ToInt32(Entry.UI.MinPause.Text);
                restartTime = endTime.AddMinutes(pauseminutes);
            }
            else
                restartTime = endTime.AddDays(100);



            Logger.WriteLine("[NRelog] We will stop the bot in " + minutes + " minutes. So we will log out at " + endTime.ToString());
            if (relogEnabled)
            {
                Logger.WriteLine("[NRelog] Relog is enabled. Pause time: " + pauseminutes + ".");
                Logger.WriteLine("[NRelog] We will relog " + relogCount + " times!");
            }
            else
                Logger.WriteLine("[NRelog] Relog is disabled.");

            Controllers.FSM.OnBotStart();
        }

        internal static bool OnInterval()
        {
            double intervalTime = 2;
            if (lastInterval.AddMinutes(intervalTime) < DateTime.Now)
            {
                timeToGo = endTime.CompareTo(DateTime.Now);
                lastInterval = DateTime.Now;
                return true;
            }
            return false;
        }



    }
}